//@ sourceURL=authority_list.js
var menu_setting = {
    check:{
        enable:false
//			chkStyle:"checkbox",
//			chkboxType:{"Y":"s","N":"s"}
    },
    data:{
        simpleData:{
            enable:true
        }
    },
    view:{
        showLine:false,
        dblClickExpand:true,
        fontCss:setFontCss_ztree
    },
    callback:{
        onClick:menuzTreeOnClick
    }
};

//添加的菜单权限
var authority_setting = {
    check: {
        enable: false
        //    chkboxType:  { "Y" : "ps", "N" : "ps" }
    },
    view: {
        showLine:false,
        dblClickExpand: true,
        fontCss:setFontCss_ztree
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
       // onClick:onDialogTreeOnClick("authorityAddList","parentId")
    }
};

var zTree,editzTree,addMenuzTree,authorityEditTree;
var DictionaryResult;
var loadDiv = null;
$(function () {
	
	setTimeout(function(){
		$(".grid-list").load(getPath() + "base/authority/add/"+0);
	}, 100);
	
	
	
	loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
    //自动获取树控件高度
	setZTreeHeight();//autoZTreeHeight();
    //初始化获取所有菜单
    initAuthorityTree();

    
    
    
    $("#refreshCacheBtn").click(function(){
    	createWindow('确定要刷新缓存？',{
            cancleEnable:true,
            callBack:function(){
            	loadDiv.showLoading();
            	post(getPath()+"base/authority/refreshAuthorityCache",{},function(result){
            		loadDiv.hideLoading();
            		createWindow("缓存刷新成功").dialog("open");
            	});
            }
        }).dialog('open');
    });

    $("#title").text("权限管理");
    //去除所有控件的虚线框
    removeBlur();
});

/**
 * 初始化树
 */
function initAuthorityTree(){
    post(getPath() + "base/authority/tree",{pid:0},
        function(treeNodes){
            treeNodes[0].open = true;
            $.fn.zTree.init($("#parentMenuTree"),authority_setting,treeNodes);
            //左侧的菜单
            var nodes = deepClone(treeNodes);
            nodes.splice(0,1);
            zTree = $.fn.zTree.init($("#menuTree"),menu_setting,nodes);
        });
}



//深度克隆
function deepClone(obj){
    var result,oClass=isClass(obj);
    //确定result的类型
    if(oClass==="Object"){
        result={};
    }else if(oClass==="Array"){
        result=[];
    }else{
        return obj;
    }
    for(key in obj){
        var copy=obj[key];
        if(isClass(copy)=="Object"){
            result[key]=arguments.callee(copy);//递归调用
        }else if(isClass(copy)=="Array"){
            result[key]=arguments.callee(copy);
        }else{
            result[key]=obj[key];
        }
    }
    return result;
}

//返回传递给他的任意对象的类
function isClass(o){
    if(o===null) return "Null";
    if(o===undefined) return "Undefined";
    return Object.prototype.toString.call(o).slice(8,-1);
}

//删除
function deleteById(){
    var selectedArray = zTree.getSelectedNodes();
    var promptWindow = null;
    if(selectedArray.length == 0){
        promptWindow = createWindow('请选择需要删除的权限');
    }else{
        promptWindow = createWindow('你确定要删除该权限',{
            cancleEnable:true,
            callBack:function(){
                var ids = '';
                for(var i=0;i<selectedArray.length;i++){
                    if(ids.length == 0){
                        ids += selectedArray[i].id;
                    }else{
                        ids += ","+selectedArray[i].id;
                    }
                }
                loadDiv.showLoading();
                post(getPath() + "base/authority/delete"+"/"+ids ,{},
                    function(result){
                		loadDiv.hideLoading();
                		var resultObject = result;
                        promptWindow.dialog("close");
                        var parentMenuTree = $.fn.zTree.getZTreeObj('parentMenuTree');
                        var menuTree = $.fn.zTree.getZTreeObj('menuTree');
                        for(var i=0;i<selectedArray.length;i++){
                            var parentMenuNode = getNodeById('parentMenuTree',selectedArray[i].id);
                            parentMenuTree.removeNode(parentMenuNode);
                            menuTree.removeNode(selectedArray[i]);
                        }
                        $(".grid-list").html("");
                        //删除成功，弹出添加成功提示框，当用户点击确定后刷新页面
                        promptWindow = createWindow("删除成功！");
                        $(".grid-list").load(getPath() + "base/authority/add/"+0);
                        /*promptWindow = createWindow(resultObject);
                        promptWindow.dialog("open");*/
                        //$("#content").load(getPath() + "base/authority/add/"+0);
                    },function(resultObject){
                    	loadDiv.hideLoading();
    	        		createWindow(resultObject).dialog('open');
    	        	});
            }
        });
    }
    promptWindow.dialog('open');
}

//菜单树的点击事件
function menuzTreeOnClick(event,treeId,treeNode){
    $("#title").text("编辑权限");
    $(".grid-list").load(getPath() + "base/authority/edit/" + treeNode.id);
}

/**
 * 获取指定id节点的数据
 * @param treeId 父节点
 * @param id id
 * @returns {*}
 */
function getNodeById(treeId, id){
    var zTree = $.fn.zTree.getZTreeObj(treeId);
    var targetNode = zTree.getNodesByFilter(function (node){
        return node.id == id;
    },true);

    return targetNode;
}
